// TypeScript file
class StarlingSwfMovieClip extends starlingswf.SwfMovieClip
{
    private frameActions: any;
    private preFrame: number;
    private completeFunc: Function;
    private completeObj: Function;
    private curFrameName: Object;

    public constructor(frames: any[], labels: any[], displayObjects: Object, ownerSwf:starlingswf.Swf)
    {
        super(frames, labels, displayObjects, ownerSwf);
        this.frameActions = {};
        this.preFrame = -1;
        this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.onRemove, this);
    }

    private onRemove(): void
    {
        this.stop();
    }

    public setFrameAction(frame: number, action: Function, actionObj: any, param: any = null):void
    {
        this.frameActions[frame] = [action, actionObj, param];
    }

    public setCompleteAction(action: Function, actionObj: any): void
    {
        this.completeFunc = action;
        this.completeObj = actionObj;
        this.addEventListener(egret.Event.COMPLETE, this.onPlayend, this);
    }

    private onPlayend(): void
    {
        if (this.completeFunc)
        {
            this.completeFunc.call(this.completeObj);
        }

    }

    public goToPlay(frame: Object): void
    {
        this.preFrame = -1;
        this.curFrameName = frame;
        this.gotoAndPlay(frame);
    }

    public update(): void
    {
        super.update();

        let curFrame: number = this.getCurFrame();
        if (this.preFrame == curFrame) return;
        this.preFrame = curFrame;
        
        if (!(this.frameActions && this.frameActions[curFrame])) return;
        let arr: Array<any> = this.frameActions[curFrame];
        if (arr[2])
        {
            arr[0].call(arr[1], arr[2]);
        }
        else
        {
            arr[0].call(arr[1]);
        }


    }

    public dispose(): void
    {
        this.stop();
        this.removeEventListener(egret.Event.COMPLETE, this.onPlayend, this);
        this.removeEventListener(egret.Event.REMOVED_FROM_STAGE, this.onRemove, this);

        if (this.parent)
        {
            this.parent.removeChild(this);
        }

        this.completeFunc = null;
        this.completeObj = null;
        this.frameActions = null;
    }

    
}